跳至主要内容

Jakarta Bean Validation

後端開發 API 時, 常需要對 Request 欄位最條件檢核, Ex: Not NULL, 僅限數字, 長度限制... 等. 尤其若串接資料庫時, 欄位檢核更是必要的.

Node.js Express 開發常搭配 Express-Validator middleware 來簡化資料欄位基本驗證的 coding 瑣事, 查找 SpringBoot 是否也有類似的 middleware 時, 發現 Jakarta Bean Validation 這個更好用的套件.

Overview

Jakarta Bean Validation

Jakarta Bean Validation Constraints

Built-In Jakarta Bean Validation Constraints 都很實用. Ref: Introduction to Jakarta Bean Validation :: Jakarta EE Tutorial :: Jakarta EE Documentation.

Constraint Description Example

@AssertFalse

The value of the field or property must be false.
@AssertFalse boolean
isUnsupported;

@AssertTrue

The value of the field or property must be true.
@AssertTrue boolean
isActive;

@DecimalMax

The value of the field or property must be a decimal value lower than or equal to the number in the value element.
@DecimalMax("30.00")
BigDecimal discount;

@DecimalMin

The value of the field or property must be a decimal value greater than or equal to the number in the value element.
@DecimalMin("5.00")
BigDecimal discount;

@Digits

The value of the field or property must be a number within a specified range. The integer element specifies the maximum integral digits for the number, and the fraction element specifies the maximum fractional digits for the number.
@Digits(integer=6, fraction=2)
BigDecimal price;

@Email

The value of the field or property must be a valid email address.
@Email
String emailaddress;

@Future

The value of the field or property must be a date in the future.
@Future
Date eventDate;

@FutureOrPresent

The value of the field or property must be a date or time in present or future.
@FutureOrPresent
Time travelTime;

@Max

The value of the field or property must be an integer value lower than or equal to the number in the value element.
@Max(10)
int quantity;

@Min

The value of the field or property must be an integer value greater than or equal to the number in the value element.
@Min(5)
int quantity;

@Negative

The value of the field or property must be a negative number.
@Negative
int basementFloor;

@NegativeOrZero

The value of the field or property must be negative or zero.
@NegativeOrZero
int debtValue;

@NotBlank

The value of the field or property must contain at least one non-white space character.
@NotBlank
String message;

@NotEmpty

The value of the field or property must not be empty. The length of the characters or array, and the size of a collection or map are evaluated.
@NotEmpty
String message;

@NotNull

The value of the field or property must not be null.
@NotNull
String username;

@Null

The value of the field or property must be null.
@Null
String unusedString;

@Past

The value of the field or property must be a date in the past.
@Past
Date birthday;

@PastOrPresent

The value of the field or property must be a date or time in the past or present.
@PastOrPresent
Date travelDate;

@Pattern

The value of the field or property must match the regular expression defined in the regexp element.
@Pattern(regexp=
"\\(\\d{3}\\)\\d{3}-\\d{4}")
String phoneNumber;

@Positive

The value of the field or property must be a positive number.
@Positive
BigDecimal area;

@PositiveOrZero

The value of the field or property must be a positive number or zero.
@PositiveOrZero
int totalGoals;

@Size

The size of the field or property is evaluated and must match the specified boundaries. If the field or property is a String, the size of the string is evaluated. If the field or property is a Collection, the size of the Collection is evaluated. If the field or property is a Map, the size of the Map is evaluated. If the field or property is an array, the size of the array is evaluated. Use one of the optional max or min elements to specify the boundaries.
@Size(min=2, max=240)
String briefMessage;

@RequestBody @Valid annotation

See Also